package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 7:45
 */

import com.wu.pojo.Materials;
import com.wu.pojo.MaterialsValues;
import com.wu.pojo.vo.MaterialsVo;
import com.wu.service.MaterialsService;
import com.wu.service.MaterialsValuesService;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author 孙
 * @date 2022/3/21 7:45
 * @version 1.0
 */
@RestController
@Api(tags="Materials模块")
@CrossOrigin
@RequestMapping("materials")
public class MaterialsController {
    @Autowired
    private MaterialsService materialsService;
    @Autowired
    private MaterialsValuesService materialsValuesService;
    @PostMapping("/insertMaterials")
    @ApiOperation(value="增加Materials")
    public R insertMaterials(@RequestBody Materials materials){
        try {
            materialsService.insertSelective(materials);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }

    @PostMapping("/insertMaterialsVo")
    @ApiOperation(value="增加MaterialsVo")
    public R insertMaterials(@RequestBody MaterialsVo materialsVo){
        try {
            materialsService.insertSelective(materialsVo);
             List<MaterialsValues> materialsValuesList = materialsVo.getMaterialsValuesList();
             if (materialsValuesList.size()>0&& materialsValuesList!=null){
                 for (MaterialsValues materialsValues : materialsValuesList) {
                     materialsValuesService.insertSelective(materialsValues);
                 }
             }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteMaterials/{id}")
    @ApiOperation(value="删除Materials")
    public R deleteMaterials(Integer id){
        try {
            materialsService.deleteByPrimaryKey(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateMaterials")
    @ApiOperation(value="更新Materials")
    public R updateMaterials(@RequestBody Materials materials){
        try {
            materialsService.updateByPrimaryKeySelective(materials);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findMaterials")
    @ApiOperation(value="查询MaterialsList")
    public R findMaterials(Materials materials){
        try {
            List<MaterialsVo> list= materialsService.findMaterials(materials);
            return R.ok().data("materialsList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findMaterialsById/{id}")
    @ApiOperation(value="通过id查询Materials")
    public R findMaterials(@PathVariable("id") Integer id){
        try {
            Materials materials= materialsService.selectByPrimaryKey(id);
            return R.ok().data("materials",materials);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
